package com.yuandian.business.monitor.controller;

import com.yuandian.api.business_monitor.po.SystemOverviewParams;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.IAppSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 说明：应用系统controller
 *
 * @Author yanjun
 * @Date 2020/12/15 8:43
 */
@RestController
@RequestMapping("/system")
@Api(value = "system", tags = "应用系统接口")
public class AppSystemController {

    @Autowired
    private IAppSystemService appSystemService;

    /**
     * 系统请求量趋势表字段Set
     */
    public static Set<String> busiSysReqTrendFields;

    /**
     * 系统请求量趋势表字段Set赋值
     */
    static {
        busiSysReqTrendFields = new HashSet<>();
        busiSysReqTrendFields.addAll(Arrays.asList("avgDealTime", "visitCount", "failVisitCount", "alarmVisitCount"));
    }


    /**
     * 查询关键系统TOP5
     *
     * @return 通用返回对象
     */
	@ApiOperation(value = "查询关键系统TOP5", notes = "查询关键系统TOP5")
    @GetMapping("/queryKeySystemTop5")
    public R queryKeySystemTop5() {
        return appSystemService.queryKeySystemTop5();
    }

    /**
     * 查询系统统计详情
     *
     * @param params
     * @return
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@ApiOperation(value = "查询系统统计详情", notes = "查询系统统计详情")
    @PostMapping("/querySystemStatisticsDetail")
    public R querySystemStatisticsDetail(@RequestBody SystemOverviewParams params) {

        return R.ok(appSystemService.querySystemStatisticsDetail(params));
    }

    /**
     * 查询用户数
     *
     * @param params
     * @return
     */
	@ApiOperation(value = "查询用户数", notes = "查询用户数")
    @PostMapping("/queryUserNum")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
    public R queryUserNum(@RequestBody SystemOverviewParams params) {

        return R.ok(appSystemService.queryUserNum(params));
    }

    /**
     * 查询异常用户数
     *
     * @param params
     * @return
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
	@ApiOperation(value = "查询异常用户数", notes = "查询异常用户数")
    @PostMapping("/queryExceptionUserNum")
    public R queryExceptionUserNum(@RequestBody SystemOverviewParams params) {

        return R.ok(appSystemService.queryExceptionUserNum(params));
    }


    /**
     * 查询全系统总览
     *
     * @param params
     * @return
     */
	@ApiOperation(value = "查询全系统总览", notes = "查询全系统总览")
    @PostMapping("/querySystemOverview")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "Integer"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 		value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 				value = "升降序:ASC / DESC", paramType = "query", 	required = true, 	dataType = "String")
	})
    public R querySystemOverview(SystemOverviewParams params) {

        return R.ok(appSystemService.querySystemOverview(params));
    }

    /**
     * 下载全系统总览
     *
     * @param params
     * @return
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "orderByColumn", 		value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 				value = "升降序:ASC / DESC", paramType = "query", 	required = true, 	dataType = "String")
	})
	@ApiOperation(value = "下载全系统总览", notes = "下载全系统总览")
    @PostMapping("/downloadSystemOverview")
    public void downloadSystemOverview(@RequestBody SystemOverviewParams params, HttpServletResponse response){
        try {
            appSystemService.downloadSystemOverview(params, response);
        } catch (Exception e) {
            e.printStackTrace();
           R.failed("导出Excel文件失败");
        }
    }

    /**
     * 系统健康度,平均处理时长,告警请求量趋势图
     * 请求量、平均响应时长、系统健康趋势-busi_sys_req_trend_m-SystemReqTrendStatTask-系统请求量趋势统计-延迟3分钟-api/modules/system/querySystemTrendList
     * @param params
     * @return
     */
	@ApiOperation(value = "系统健康度,平均处理时长,告警请求量趋势图", notes = "系统健康度,平均处理时长,告警请求量趋势图")
    @PostMapping("/querySystemTrendList")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", 	required = true, 	dataType = "Integer"),
	})
    public R systemTrendList(SystemOverviewParams params){
        if (params == null) {
            return R.failed("参数不能为空");
        }
        if (params.getSystemId() == null) {
            return R.failed("系统Id不能为空");
        }
        return R.ok(appSystemService.systemTrendList(params));
    }
}

